<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <SearchForm :searchData="searchData" @handleReset="handleReset" @handleSearch="handleSearch"/>
    <TableList :listData="listData" :total="total" :pagination="pagination" :dataLoading="dataLoading"
               @getData="getData" @deleteLevel="deleteLevel"  @handleEdit="handleEdit" @openForm="openForm" />

    <DialogForm :visible="visible" :data="data" :planData="planData" :title="title"  @handleFormClose="handleFormClose"
    @handleAdd="handleAdd" @handleEditForm="handleEditForm"/>
  </div>
</template>

<script setup lang="ts">
import DialogForm from '@/pages/serve/plan/grade/components/DialogFrom.vue'
import SearchForm from '@/pages/serve/plan/grade/components/SearchForm.vue'
import TableList from '@/pages/serve/plan/grade/components/TableList.vue'
import {
  getLevelList,
  levelDelete,
  getLevelDetails,
  getnursAllPlanList,
  planAdd,
  levelAdd,
  levelUpdate
} from '@/api/serve'
import { onMounted, ref } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
const dataLoading = ref(false)
const pagination = {
  pageNum: 1,
  pageSize: 5,
  name:'',
  status:''
}
const total = ref(0)
const listData = ref([])
const visible = ref(false)
const data=ref<any>({})
const title = ref('添加护理等级')
const planData = ref([])
// -------------搜索----------------
const searchData = ref<any>({})
function handleReset(){
  console.log("重置",pagination)
  searchData.value = {}
  pagination.name =searchData.value.name
  pagination.status = searchData.value.status
  getData()
}

function handleSearch(){
  console.log("搜索" ,pagination)
  pagination.name =searchData.value.name
  pagination.status = searchData.value.status
  getData()
}





// -------------表格----------------


async function getData(){
  const res:any = await getLevelList(pagination)
  listData.value = res.data.records
  pagination.pageNum = res.data.page
  pagination.pageSize = res.data.pageSize
  total.value = res.data.total
}
async function getLevelData(){
  await getnursAllPlanList().then((res:any) => {
    planData.value = res.data
  })
}
async function deleteLevel(id:number){
  console.log("删除id",id)
  const res:any = await levelDelete(id)
  if(res.code === 200){
    MessagePlugin.success(res.msg)
    getData()
  }else {
    MessagePlugin.error(res.msg)
  }
}
async function handleEdit(id:number){
  visible.value = true
  title.value = '编辑'
  await getLevelDetails(id).then((res:any) => {
    data.value = res.data
  })
}
function openForm(){
  visible.value = true
  title.value = '新增'
}
function handleFormClose(){
  console.log("关闭弹窗")
  visible.value = false
  data.value = {}
  title.value=''
}

async function handleAdd(formData:any){
  console.log("新增123 : formData",formData)
  await levelAdd(formData).then((res:any) => {
    if(res.code === 200){
      MessagePlugin.success(res.msg)
      handleFormClose()
      getData()
    }else {
      MessagePlugin.error(res.msg)
    }
  })
}

async function handleEditForm(formData:any){
  console.log("编辑 : formData",formData)
  await levelUpdate(formData).then((res:any) => {
    if(res.code === 200){
      MessagePlugin.success(res.msg)
      handleFormClose()
      getData()
    }else {
      MessagePlugin.error(res.msg)
    }
  })
}

onMounted(async () => {
  getData()
  getLevelData()
})
</script>
